import {
	IAbility,
	IActionResult,
	IContext,
	IParam,
	IUIService,
	IViewLogicHelper,
	IViewLogicInput,
} from '@/core';

/**
 * 视图逻辑辅助类
 */
export class ViewLogicHelper implements IViewLogicHelper {
	/**
	 * 单例服务
	 *
	 * @protected
	 * @static
	 * @type {IViewLogicHelper}
	 * @memberof ViewLogicHelper
	 */
	private static service: IViewLogicHelper;

	/**
	 * 获取实例
	 *
	 * @static
	 * @return {*}  {IViewLogicHelper}
	 * @memberof ViewLogicHelper
	 */
	static getInstance(): IViewLogicHelper {
		if (!this.service) {
			this.service = new ViewLogicHelper();
		}
		return this.service;
	}

	/**
	 * 执行视图逻辑
	 *
	 * @template T
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @param {MouseEvent} event
	 * @param {T} ability
	 * @param {IViewLogicInput} inputParams
	 * @return {*}  {Promise<IActionResult>}
	 * @memberof ViewLogicHelper
	 */
	public async executeViewLogic<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam[],
		event: MouseEvent,
		ability: T,
		inputParams: IViewLogicInput
	): Promise<IActionResult> {
		const { actionTag, predefinedType } = inputParams;
		if (predefinedType) {
			return await App.getGlobalActionHelper().executeUIAction(
				predefinedType,
				context,
				viewParams,
				data,
				event,
				ability
			);
		}
		const { actionDECodeName } = inputParams;
		if (actionDECodeName) {
			const uiService: IUIService = await App.getUIService(
				actionDECodeName,
				context
			);
			if (!uiService) {
				return { ok: false, data: null };
			}
			return await uiService.executeUIAction(
				actionTag,
				context,
				viewParams,
				data,
				event,
				ability
			);
		}
		return { ok: false, data: null };
	}
}
